package com.tuocent.dagv2.config;

import com.tuocent.dagv2.common.filter.CacheRequestBodyFilter;
import com.tuocent.dagv2.common.filter.WebFilterOrderEnum;
import jakarta.servlet.Filter;
import jakarta.servlet.MultipartConfigElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Value("${location.temp-dir}")
    private String tempDir;

    @Autowired
    private AppInterceptor interceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludeURLS = new ArrayList<>();
        excludeURLS.add("/files/getLogo");
        excludeURLS.add("/home/register");
        excludeURLS.add("/favicon.ico");
        excludeURLS.add("/systemManage/saveRegisterInfo");
        excludeURLS.add("/db/**");
        excludeURLS.add("/error");
        excludeURLS.add("/home/error");
        excludeURLS.add("/home/cliConn");
        excludeURLS.add("/account/login");
        excludeURLS.add("/account/validateCode");
        excludeURLS.add("/account/userLogin");

        excludeURLS.add("/RestApi/system/checkServer");
        excludeURLS.add("/RestApi/system/userLogin");

        excludeURLS.add("/images/**");
        excludeURLS.add("/images/ico/**");
        excludeURLS.add("/pdfjs/**");
        excludeURLS.add("/**/*.css");
        excludeURLS.add("/**/font/**");
        excludeURLS.add("/**/*.js");
        excludeURLS.add("/layui/**");
        excludeURLS.add("/bpmnJS/*.css");
        excludeURLS.add("/**/*.xlsx");
        registry.addInterceptor(interceptor).addPathPatterns("/**").excludePathPatterns(excludeURLS);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**")
                .addResourceLocations("classpath:/static");
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        File tmpDirFile = new File(tempDir);
        // 判断文件夹是否存在
        if (!tmpDirFile.exists()) {
            tmpDirFile.mkdirs();
        }
        factory.setLocation(tempDir);
        return factory.createMultipartConfig();
    }

    @Bean
    public FilterRegistrationBean<CacheRequestBodyFilter> requestBodyCacheFilter() {
        return createFilterBean(new CacheRequestBodyFilter(), WebFilterOrderEnum.REQUEST_BODY_CACHE_FILTER);
    }

    public static <T extends Filter> FilterRegistrationBean<T> createFilterBean(T filter, Integer order) {
        FilterRegistrationBean<T> bean = new FilterRegistrationBean<>(filter);
        bean.setOrder(order);
        return bean;
    }


}
